package com.hxh.controller;

import com.hxh.entity.Project;
import com.hxh.entity.ProjectTablePageConfig;
import com.hxh.entity.output.ApiResult;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.springBoot.GenerationSpringBootAdapter;
import com.hxh.generater.springBoot.GenerationTableInfo;
import com.hxh.generater.vue.GenerationVueAdapter;
import com.hxh.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * @ProjectName: AICode
 * @Package: com.example.controller
 * @ClassName: TableConfigController
 * @Author: HXH
 * @Description: 项目表配置
 * @Date: 2023/12/11 9:32
 */
@RestController
@RequestMapping("/api/createCode")
@Slf4j
public class TableToCodeController {
    @Autowired
    ProjectService projectService;

    @Autowired
    ProjectTablePageConfigService projectTablePageConfigService;

    @Autowired
    ProjectTableFiledConfigService projectTableFiledConfigService;

    @Autowired
    GenerationSpringBootAdapter generationSpringBootAdapter;

    @Autowired
    TemplateConfigService templateConfigService;

    @Autowired
    GenerationVueAdapter generationVueAdapter;

    @Autowired
    ProjectConfigRecordService projectConfigRecordService;

    /**
     * @param projectId:
     * @param tableName:
     * @param type:      service / entity / dao / mapper / controller
     * @return com.example.entity.output.ApiResult
     * @author HXH
     * @description:
     * @date 2023/12/11
     **/
    @RequestMapping(value = "/createTableCRUD", produces = "application/json;charset=utf-8")
    public ApiResult createTableCRUD(@RequestParam Integer projectId, @RequestParam Integer datasourceId,
                                     @RequestParam String tableName,@RequestParam String type) {
        Project project = projectService.selectById(projectId);
        if (project == null) {
            return ApiResult.fail("项目不存在！");
        }
        try {
            ClassInfo classInfo = GenerationTableInfo.getTableInfo(project, datasourceId, tableName);
            if (type.contains("service")) {
                generationSpringBootAdapter.generationServiceClass(project, classInfo);
            }
            if (type.contains("serviceImpl")) {
                generationSpringBootAdapter.generationServiceImplClass(project, classInfo);
            }
            if (type.contains("entity")) {
                generationSpringBootAdapter.generationEntity(project, classInfo);
            }
            if (type.contains("dao")) {
                generationSpringBootAdapter.generationDaoClass(project, classInfo);
            }
            if (type.contains("mapper")) {
                generationSpringBootAdapter.generationMapper(project, classInfo);
            }
            if (type.contains("controller")) {
                generationSpringBootAdapter.generationControllerClass(project, classInfo);
            }
            String table = classInfo.getProjectDatasource().getMysqlDatabaseName() + "." + tableName;
            projectConfigRecordService.clearBackRebuildFlag(projectId,table);
            return ApiResult.ok("生成crud代码成功");
        } catch (Exception e) {
            log.error("生成crud代码失败！", e);
            return ApiResult.fail(e);
        }
    }

    @PostMapping(value = "createTableMenu")
    public ApiResult createTableMenu(@RequestParam Integer projectId,@RequestParam Integer datasourceId,
                                     @RequestParam String tableName) {
        Project project = projectService.selectById(projectId);
        if (project == null) {
            return ApiResult.fail("项目不存在！");
        }
        try {
            ClassInfo classInfo = GenerationTableInfo.getTableInfo(project, datasourceId, tableName);
            generationSpringBootAdapter.initTableMenu(project, classInfo);
            return ApiResult.ok("生成菜单成功");
        } catch (Exception e) {
            log.error("生成菜单失败", e);
            return ApiResult.fail("生成菜单失败," + e.getMessage());
        }
    }

    @PostMapping(value = "processAdditionTableMethod")
    public ApiResult processAdditionTableMethod(@RequestParam Integer projectId,@RequestParam Integer datasourceId,
                                                @RequestParam String tableName,
                                                @RequestParam String className, @RequestParam String methodName) {
        Project project = projectService.selectById(projectId);
        if (project == null) {
            return ApiResult.fail("项目不存在！");
        }
        try {
            ClassInfo classInfo = GenerationTableInfo.getTableInfo(project, datasourceId, tableName);
            generationSpringBootAdapter.processAdditionTableMethod(project, classInfo, className, methodName);
            return ApiResult.ok("操作成功");
        } catch (InvocationTargetException e) {
            log.error("操作异常", e);
            return ApiResult.fail("操作失败: " + e.getTargetException().getMessage());
        } catch (Exception e) {
            log.error("操作异常", e);
            return ApiResult.fail("操作失败: " + e.getMessage());
        }
    }

    /**
     * @param projectId:
     * @param tableName:
     * @param type:      api / view / router
     * @return com.example.entity.output.ApiResult
     * @author HXH
     * @description:
     * @date 2023/12/11
     **/
    @RequestMapping(value = "createTableVueCode", produces = "application/json;charset=utf-8")
    public ApiResult createTableVueCode(@RequestParam Integer projectId, @RequestParam String tableName,
                                        @RequestParam String type, @RequestParam Integer datasourceId) {
        Project project = projectService.selectById(projectId);
        if (project == null) {
            return ApiResult.fail("项目不存在！");
        }
        try {
            ClassInfo classInfo = GenerationTableInfo.getTableInfo(project, datasourceId, tableName);
            ProjectTablePageConfig projectTablePageConfig = new ProjectTablePageConfig();
            projectTablePageConfig.setProjectId(projectId);
            projectTablePageConfig.setTableName(tableName);
            if (type.contains("api")) {
                generationVueAdapter.generationApi(project, classInfo);
            }
            if (type.contains("view")) {
                generationVueAdapter.generationView(project, classInfo);
            }
            String table = classInfo.getProjectDatasource().getMysqlDatabaseName() + "." + tableName;
            projectConfigRecordService.clearFrontRebuildFlag(projectId,table);
            return ApiResult.ok("生成代码成功");
        } catch (RuntimeException e) {
            log.error("初始化页面失败", e);
            return ApiResult.fail("生成代码失败:" + e.getMessage());
        } catch (Exception e) {
            log.error("初始化页面失败", e);
            return ApiResult.fail(e);
        }
    }


}
